package com.etoak.service.impl;

import com.etoak.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public boolean like(String key, String username) {
        Boolean hasKey = stringRedisTemplate.opsForHash().hasKey(key, username);
        if(hasKey){
            //如果有则删除掉
            stringRedisTemplate.opsForHash().delete(key,username);

        }else {
            //如果没有则添加到redis中
            stringRedisTemplate.opsForHash().put(key,username,"1");;
        }
        return !hasKey;
    }

    @Override
    public Long getNumber(String key) {
        Long size = stringRedisTemplate.opsForHash().size(key);
        return size;
    }
}
